<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller\Backend;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\Role;
use App\Request\Backend\Admin\AdminAddRequest;
use App\Request\Backend\Admin\AdminEditRequest;
use App\Resource\BaseResource;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

class Admin extends AbstractController
{
    use \App\Traits\PwdTool;

    public function index(RequestInterface $request, ResponseInterface $response)
    {
        $where = [];

        if ($request->input('user_name')) {
            $where[] = ['user_name', 'like', '%' . $request->input('user_name') . '%'];
        }

        if ($request->input('nick_name')) {
            $where[] = ['nick_name', 'like', '%' . $request->input('nick_name') . '%'];
        }

        $list = \App\Model\Admin::where($where)->paginate();

        return \App\Resource\Backend\BaseResource::collection($list)->additional(['code' => 0, 'msg' => '成功']);
    }

    public function adminAdd(AdminAddRequest $request)
    {
        $data = $request->all();
        $data['password'] = $this->makePassword($request->input('password'));

        $role = Role::find($request->input('role_id'));

        $data['role_name'] = $role->role_name;
        \App\Model\Admin::create($data);

        return new BaseResource();
    }

    public function adminEdit(AdminEditRequest $request)
    {
        $data = $request->all();
        if ($request->input('password')) {
            $data['password'] = $this->makePassword($request->input('password'));
        }

        if($request->input('id') == 1) {
            $role = Role::find(1);
            $data['role_name'] = $role->role_name;
        }else{
            $role = Role::find($request->input('role_id'));
            $data['role_name'] = $role->role_name;
        }

        \App\Model\Admin::find($request->input('id'))->update($data);

        return new BaseResource();
    }

    public function adminDel($id)
    {
        if ($id == 1) {
            throw new BusinessException(ErrorCode::CUSTOM_ERROR . '超级管理员不允许删除');
        }
        \App\Model\Admin::find($id)->delete();

        return new BaseResource();
    }

    public function updatePwd(RequestInterface $request)
    {
        $pwd1 = $request->input('password');
        $pwd2 = $request->input('passwordTwo');
        $data = [];
        if (strlen($pwd1) != 0 || strlen($pwd2) != 0) {
            if ($pwd1 != $pwd2) {
                throw new BusinessException(ErrorCode::ADMIN_PWD_UPDATE2_ERROR);
            }

            $data['password'] = $this->makePassword($request->input('password'));
        }

        $data['user_name'] = $request->input('user_name');
        $data['avatar'] = $request->input('avatar');

        \App\Model\Admin::find($this->session->get('admin_user')->id)->update($data);
        return new BaseResource();
    }

    public function adminCurEdit(AdminEditRequest $request)
    {
        $data = $request->all();
        if ($request->input('password')) {
            $data['password'] = $this->makePassword($request->input('password'));
        }else{
            unset($data['password']);
        }

        unset($data['login_name']);

        \App\Model\Admin::find($this->session->get('admin_user')->id)->update($data);

        return new BaseResource();
    }
}
